﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FeedAgregator;
using DDNY.WebPortals.Common;

public partial class ViewItem : System.Web.UI.Page
{


    public int FeedItemId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            foreach (string key in Request.QueryString)
            {
                switch (key.ToLower())
                {

                    case "id":
                        FeedItemId = int.Parse(Request.QueryString[key]);
                        break;
                    default:
                        break;
                }
            }

            if (Request.QueryString["title"] == null)
            {
                using (FeedSignalDataContext ctx = new FeedSignalDataContext())
                {
                    FeedItem item = (from feedItems in ctx.FeedItems
                                     where feedItems.ID == FeedItemId
                                     select feedItems).SingleOrDefault();
                    string urlTitle = UrlHelper.GenerateGoodTitleQueryString(item.Title);

                    string redirectUrl = Request.Url.AbsoluteUri + "&title=" + urlTitle;
                    // new line here
                    HttpContext.Current.Response.Clear();
                    //
                    HttpContext.Current.Response.Status = "301 Moved Permanently";
                    HttpContext.Current.Response.AddHeader("Location", redirectUrl);
                    Response.End();

                }
            }


            using (FeedSignalDataContext ctx = new FeedSignalDataContext())
            {
                FeedItem item = (from feedItems in ctx.FeedItems
                                 where feedItems.ID == FeedItemId
                                 select feedItems).SingleOrDefault();


                ReadHistory historyToAdd = new ReadHistory();
                historyToAdd.FeedItemID = item.ID;
                historyToAdd.DateReaded = DateTime.Now;
                ctx.ReadHistories.InsertOnSubmit(historyToAdd);
                ctx.SubmitChanges();
                if (item.ItemContent != null)
                {
                    item.ItemContent = CommonUtils.StripHtml(item.ItemContent);
                    item.ItemContent = CommonUtils.SanitizeContent(item.ItemContent, false);

                    if (item.ItemContent.Length > 100)
                    {
                        ItemContentDiv.InnerHtml = item.ItemContent.Substring(0, 100);
                    }
                    else
                    {
                        ItemContentDiv.InnerHtml = item.ItemContent;
                    }
                    ItemContentDiv.InnerHtml += " Тагове ";
                    foreach (FeedItemTagRelation tagRelItem in item.FeedItemTagRelations)
                    {
                        ItemContentDiv.InnerHtml += " " + tagRelItem.Tag.TagName;
                    }
                    ItemContentDiv.Visible = true;
                }


                this.Title = item.Title + " - " + item.Feed.DisplayName + " - " + item.PubDate.ToString("dd.MM.yyyy") + " - " + item.Feed.FeedCategory.CategoryName;
                ExcerptTitleLabel.Text = "" + item.Title;

                LinkToSource.Text = "Към целия текст";
                LinkToSource.NavigateUrl = "~/ViewPage.aspx?id=" + item.ID.ToString();
                LinkToSource.Attributes.Add("rel", "nofollow");

                LoadSimilarPosts(item);
                //Add to Reading History

                //BackLink.Attributes.Add("onclick", "window.history.go(-1);");
            }


        }


    }

    private void LoadSimilarPosts(FeedItem item)
    {
        List<FeedItem> relatedPosts = DataAccessHelper.GetRelatedPosts(item);
        foreach (FeedItem relatedPost in relatedPosts)
        {
            if (relatedPost.ItemContent != null && relatedPost.ItemContent.Length > 0)
            {
                relatedPost.ItemContent = CommonUtils.SanitizeContent(relatedPost.ItemContent, false);
                relatedPost.ItemContent = CommonUtils.GetExcerpt(relatedPost.ItemContent);
                relatedPost.ItemContent = CommonUtils.StripHtml(relatedPost.ItemContent);
            }
        }
        RelatedPostsListView.DataSource = relatedPosts;
        RelatedPostsListView.DataBind();
    }
}
